package com.qidian.mes.store.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidian.common.pojo.MESResult;
import com.qidian.mes.pojo.Store;
import com.qidian.mes.store.mapper.StoreMapper;
import com.qidian.mes.store.service.StoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 任超
 * Time 2024/10/25 18:40
 */
@Service
public class StoreServiceImpl implements StoreService {

    @Autowired
    private StoreMapper storeMapper;
    @Override
    public MESResult selectList(Store store, int page, int limit) {
        PageHelper.startPage(page,limit);
        List<Store> storeList = storeMapper.list(store);
        PageInfo<Store> pageInfo = new PageInfo<>(storeList);
        return MESResult.success(pageInfo);
    }

    @Override
    public MESResult addStoreSetting(Store store) {
        if (storeMapper.selectById(store.getStoreId()) != null)
            return MESResult.error();
        if (storeMapper.addStoreSetting(store) == 1)
            return MESResult.success(store);
        return MESResult.error();
    }

    @Override
    public MESResult updateStoreSetting(Store store) {
        Store temp = storeMapper.selectByCode(store.getStoreCode());
        if (temp!= null && !temp.getStoreCode().equals(store.getStoreCode()))
            return MESResult.error();
        else
            storeMapper.updateSetting(store);
        return MESResult.success();
    }

    @Override
    public MESResult selectById(Long id) {
//        Store temp = storeMapper.selectById(id);
//        if (temp == null)
//            return MESResult.error();
//        else
//            return MESResult.success(temp);
        return MESResult.success(storeMapper.selectById(id));
    }

    @Override
    public MESResult updateStoreStatus(Long id) {
        return MESResult.success(storeMapper.updateStoreStatus(id));
    }
}
